package com.shameit.goodqrinfomanager.ui.activity.offline;

import android.app.Application;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.shameit.goodqrinfomanager.adapter.OfflineListAdapter;
import com.shameit.goodqrinfomanager.bean.enity.OfflinListBean;
import com.shameit.goodqrinfomanager.bean.enity.UploadBarCode;
import com.shameit.goodqrinfomanager.bean.litepal.CangKu;
import com.shameit.goodqrinfomanager.bean.litepal.CangWei;
import com.shameit.goodqrinfomanager.bean.litepal.ChanPin;
import com.shameit.goodqrinfomanager.bean.litepal.HuoMa;
import com.shameit.goodqrinfomanager.bean.litepal.PiCi;
import com.shameit.goodqrinfomanager.bean.litepal.XiangMa;
import com.shameit.goodqrinfomanager.source.http.DataRepository;
import com.shameit.goodqrinfomanager.source.http.RequestBodyFactory;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class OfflineManagementViewModel extends BaseViewModel<DataRepository> {
    public OfflineListAdapter adapter;

    public OfflineManagementViewModel(@NonNull Application application, DataRepository model) {
        super(application, model);
    }

    public List<OfflinListBean> getListBean(){
        List<OfflinListBean> list= new ArrayList<>();
        List<PiCi> piciList = LitePal.order("code").find(PiCi.class);
        for (PiCi piCi:piciList){
            int piciCount = LitePal.where("piciCode = ?",piCi.getCode()).count(XiangMa.class);
            List<XiangMa> xiangMaList = LitePal.where("piciCode = ?",piCi.getCode()).find(XiangMa.class);
            int huoListCount = 0;
            for (XiangMa xiangMa:xiangMaList){
                int huoCount =  LitePal.where("xiangCode = ?",xiangMa.getCode()).count(HuoMa.class);
                huoListCount +=huoCount;
            }
            List<UploadBarCode> uploadList = new ArrayList<>();
            for (XiangMa xiangMa:xiangMaList){
                List<HuoMa>  huolist =  LitePal.where("xiangCode = ?",xiangMa.getCode()).find(HuoMa.class);
                for (HuoMa huoMa : huolist) {
                    UploadBarCode bean = new UploadBarCode();
                    bean.setCode(huoMa.getCode());
                    bean.setParentsCode(xiangMa.getCode());
                    bean.setProductCode(huoMa.getChanpinCode());
                    bean.setProductDate(huoMa.getOperatorTime());
                    uploadList.add(bean);
                }
            }
            OfflinListBean bean = new OfflinListBean();
            bean.setDanCode(piCi.getCode());
            bean.setXiangNumber(String.valueOf(piciCount));
            bean.setMaNumber(String.valueOf(huoListCount));
            bean.setUpState(false);
            bean.setList(uploadList);
            bean.setTime(OfflineManagementActivity.dateTimeFormat.format(new Date()));
            list.add(bean);
        }
        return list;
    }


    public String[] loadCangkuItem(){
        List<CangKu> cangKuList = LitePal.order("warehouseNo").find(CangKu.class);
        String[] ckArr = new String[cangKuList.size()];
        for (int i = 0; i < cangKuList.size(); i++) {
            ckArr[i] = cangKuList.get(i).getWarehouseNo();
        }
        return ckArr;
    }

    public String[] loadCangweiItem(String cangkucode){
        List<CangWei> cangWeiList = LitePal.where("warehouseNo = ? ", cangkucode).order("placeNo").find(CangWei.class);
        String[] cwArr = new String[cangWeiList.size()];
        for (int i = 0; i < cangWeiList.size(); i++) {
            cwArr[i] = cangWeiList.get(i).getPlaceNo();
        }
        return cwArr;
    }




    public void uploadBarCodeBind(OfflinListBean bean){
        List list = bean.getList();
        addSubscribe(
                model.uploadBarBind(RequestBodyFactory.getRequestBody(new Gson().toJson(list)))
                .compose(RxUtils.Obs_io_main())
                .compose(RxUtils.handle_result())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                })
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        dismissDialog();
                        if (aBoolean) {
                            ToastUtils.showShort("上传成功");
                        } else {
                            ToastUtils.showShort("上传失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        dismissDialog();
                        ToastUtils.showShort("失败!错误码：" + throwable.getMessage());
                    }
                })
        );
    }
}
